# -*- encoding:utf-8 -*-
# @Time: 2022/11/1 21:25
# @Author : renyuxuan
# @Email: renyuxuan@maqu.com
# @File: serializers.py
# @Software: PyCharm
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
from users.serializers import UserSerializer
from .models import *

class ClassesSerializer(ModelSerializer):
    memberDetail = SerializerMethodField()

    class Meta:
        model = Classes
        exclude = ['is_delete']

    def get_memberDetail(self,instance):
        # 分类
        members = UserSerializer(instance.member.all(), many=True).data # 获取当前班级所有的用户数据，并序列化
        return {
            'teachers':[i['username'] for i in members if i['is_superuser'] or 1 in i['groups']],
            'students':[i['username'] for i in members if 2 in i['groups']]
        }

class CreateUserializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['username','first_name','email']
        extra_kwargs = {
            'username':{'required':True},#用户名可重名
            'email':{'required':True}
        }
